package it.giuseppefrattura.wwdefence;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

//import android.util.Log;


public class Mattonella {

	private Sprite sprite;
	private boolean created=false;
	private boolean autofire=true;
	
	public Sprite getSprite(){
		return sprite;
	}
	
	public Mattonella() {
		
	    sprite= new Sprite(0, 0, MainActivity.getSharedInstance().mMatt , MainActivity.getSharedInstance().getVertexBufferObjectManager())
	    
	    {
			@Override 
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {	 
				if (!created){
					Soldier s1= new Soldier( 21.0f , 9.0f);
					this.attachChild(s1.getSprite());
					created=true;
					shootTimeHandler(s1,this.getX(),this.getY());
				}
				return true;
			}
		};
	}
	
	public void shootTimeHandler(final Soldier s1, final float X, final float Y)
    {          
        final TimerHandler shootTimerHandler = new TimerHandler(1,true,new ITimerCallback()
            {                      
                @Override
                public void onTimePassed(final TimerHandler pTimerHandler) {if (autofire) s1.shoot(X+5,Y+30);}
            });    
        MainActivity.getSharedInstance().getEngine().registerUpdateHandler(shootTimerHandler);
    }

	public void stopFire(){
		autofire=false;
	}

}